library(testthat)

# remove.packages("EValue")
# install.packages("EValue")
library(EValue)

# detach("package:EValue", unload = TRUE)
# setwd("~/Dropbox/Personal computer/Independent studies/R packages/EValue package (git)/evalue_package/EValue/R")
# setwd("~/Box Sync/jlee/Maya/evalue/EValue/R")
# source("~/Box Sync/jlee/Maya/evalue/EValue/R/meta-analysis.R")


library(devtools)
library(dplyr)
library(ICC)
library(MetaUtility)
library(ggplot2)
library(boot)
library(here)

Below tests run on 01/07/21 with github code pull on 01/06/21:

# make file paths relative so they run on other machines
setwd(here())
source("helper_testthat.R")

# setwd("~/Box Sync/jlee/Maya/metasens_website/Main site/tests_human_inspection")
# source("helper_testthat.R")


# source("~/Box Sync/jlee/Maya/evalue/EValue/tests/helper_testthat.R")
# source("~/Box Sync/jlee/Maya/evalue/EValue/R/meta-analysis.R")
# setwd("~/Box Sync/jlee/Maya/evalue/tests_human_inspection/")

test1 gbc_prepped.csv file - correct

d = read.csv("Datasets for website test/gbc_prepped.csv", stringsAsFactors = FALSE)

# note: log(0.9) = -0.1053605
confounded_meta(method="calibrated",
                q = log(.9),
                r = 0.1,
                muB = log(1),
                tail = "below",
                yi.name = "yi",
                vi.name = "vi",
                dat = d,
                R = 500)
## [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
## Prop is already less than or equal to r even with no confounding, so Tmin and Gmin are simply equal to 1. No confounding at all is required to make the specified shift.
##   Value        Est         SE CI.lo      CI.hi
## 1  Prop 0.02358491 0.01459543     0 0.08056641
## 2  Tmin 1.00000000 0.00000000     1         NA
## 3  Gmin 1.00000000 0.00000000     1         NA
### R output:
# [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
# Prop is already less than or equal to r even with no confounding, so Tmin and Gmin are simply equal to 1. No confounding at all is required to make the specified shift.
#   Value        Est         SE CI.lo      CI.hi
# 1  Prop 0.02358491 0.01553758     0 0.08018868
# 2  Tmin 1.00000000 0.00000000     1         NA
# 3  Gmin 1.00000000 0.00000000     1         NA
# Warning message:
# In norm.inter(t, adj.alpha) : extreme order statistics used as endpoints


sens_plot(method="calibrated",
          type = "line",
          q = log(0.9),
          tail = "below",
          Bmin = log(1),
          Bmax = log(4),
          yi.name = "yi",
          vi.name = "vi",
          dat = d,
          R = 500)
## Warning: Problem with `mutate()` input `..1`.
## ℹ extreme order statistics used as endpoints
## ℹ Input `..1` is `...[]`.
## ℹ The error occurred in row 28.
## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints
## Warning: Problem with `mutate()` input `..1`.
## ℹ extreme order statistics used as endpoints
## ℹ Input `..1` is `...[]`.
## ℹ The error occurred in row 35.
## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints
## Warning: Problem with `mutate()` input `..1`.
## ℹ extreme order statistics used as endpoints
## ℹ Input `..1` is `...[]`.
## ℹ The error occurred in row 36.
## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints
## Some of the pointwise confidence intervals were not estimable via bias-corrected and accelerated bootstrapping, so the confidence band on the plot may not be shown for some values of the bias factor. This usually happens at values with a proportion estimate close to 0 or 1. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.

### Website output: 

knitr::include_graphics("mytest-expected/001.png")

test2 gbc_prepped.csv file - correct

d = read.csv("Datasets for website test/gbc_prepped.csv")

# note: log(0.5) = -0.6931472
confounded_meta(method="calibrated",
                q = log(0.5),
                r = 0.5,
                muB = log(0.5),
                tail = "above",
                yi.name = "yi",
                vi.name = "vi",
                dat = d,
                R = 500)
## Error in confounded_meta(method = "calibrated", q = log(0.5), r = 0.5, : Must have muB > 0. Use the muB.toward.null argument instead if you want to consider bias away from the null. See Details.
### R output:
# Error in confounded_meta(method = "calibrated", q = log(0.5), r = 0.5,  : 
#   Must have muB > 0. Use the muB.toward.null argument instead if you want to consider bias away from the null. See Details.

sens_plot(method="calibrated",
          type = "line",
          q = log(.5),
          tail = "above",
          Bmin = log(1),
          Bmax = log(6),
          yi.name = "yi",
          vi.name = "vi",
          dat = d,
          R = 500)
## [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
## Warning: Problem with `mutate()` input `..1`.
## ℹ extreme order statistics used as endpoints
## ℹ Input `..1` is `...[]`.
## ℹ The error occurred in row 2.
## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints
## Warning: Problem with `mutate()` input `..1`.
## ℹ extreme order statistics used as endpoints
## ℹ Input `..1` is `...[]`.
## ℹ The error occurred in row 3.
## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints
## Warning: Problem with `mutate()` input `..1`.
## ℹ extreme order statistics used as endpoints
## ℹ Input `..1` is `...[]`.
## ℹ The error occurred in row 4.
## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints
## Warning: Problem with `mutate()` input `..1`.
## ℹ extreme order statistics used as endpoints
## ℹ Input `..1` is `...[]`.
## ℹ The error occurred in row 5.
## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints
## Warning: Problem with `mutate()` input `..1`.
## ℹ extreme order statistics used as endpoints
## ℹ Input `..1` is `...[]`.
## ℹ The error occurred in row 8.
## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints
## Some of the pointwise confidence intervals were not estimable via bias-corrected and accelerated bootstrapping, so the confidence band on the plot may not be shown for some values of the bias factor. This usually happens at values with a proportion estimate close to 0 or 1. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.

### R output:
# Some of the pointwise confidence intervals were not estimable via bias-corrected and accelerated bootstrapping, so the confidence band on the plot may not be shown for some values of the bias factor. This usually happens at values with a proportion estimate close to 0 or 1. Otherwise, you can try increasing R.
# Warning messages:
# 1: In norm.inter(t, adj.alpha) :
#   extreme order statistics used as endpoints
# 2: In norm.inter(t, adj.alpha) :
#   extreme order statistics used as endpoints
# 3: In norm.inter(t, adj.alpha) :
#   extreme order statistics used as endpoints
# 4: In norm.inter(t, adj.alpha) :
#   extreme order statistics used as endpoints

### Website output (passing on log scale): 
knitr::include_graphics("mytest-expected/002.png")

### Website output (passing on RR scale): 
knitr::include_graphics("mytest-expected/003.png")

test3 gbc_prepped.csv file - correct

d = read.csv("Datasets for website test/gbc_prepped.csv")

## get error for column name
confounded_meta(method="calibrated",
                q = log(.5),
                r = 0.5,
                muB = log(1.5),
                tail = "above",
                yi.name = "yi",
                vi.name = "vyi",
                dat = d,
                R = 2000)
## Error in Phat_causal(q = q, B = muB, tail = tail, muB.toward.null = muB.toward.null, : dat does not contain a column named vi.name
### R output:
 # Error in Phat_causal(q = q, B = muB, tail = tail, muB.toward.null = muB.toward.null,  : 
 #  dat does not contain a column named vi.name 

### Website output: 
knitr::include_graphics("mytest-expected/004.png")

test4 flegal_prepped.csv file - correct

d = read.csv("Datasets for website test/flegal_prepped.csv")

## on log-RR scale:
# note: log(0.5) = -0.6931472
confounded_meta(method= "calibrated",
                q = -0.6931472,
                r = 0.5,
                muB = 0.5,
                tail = "above",
                yi.name = "yi",
                vi.name = "vi",
                dat = d,
                R = 500)
## [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
## The confidence interval and/or standard error for the proportion were not estimable via bias-corrected and accelerated bootstrapping. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.
##   Value      Est         SE    CI.lo    CI.hi
## 1  Prop 1.000000 0.00000000       NA       NA
## 2  Tmin 1.834157 0.02989419 1.786808 1.895639
## 3  Gmin 3.071078 0.06210765 2.972504 3.198639
### R output:
# [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
# The confidence interval and/or standard error for the proportion were not estimable via bias-corrected and accelerated bootstrapping. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.
#   Value      Est         SE    CI.lo    CI.hi
# 1  Prop 1.000000 0.00000000       NA       NA
# 2  Tmin 1.834157 0.03195267 1.783244 1.905911
# 3  Gmin 3.071078 0.06635321 2.965072 3.219907

sens_plot(method= "calibrated",
          type = "line",
          q = -0.6931472,
          tail = "above",
          Bmin = 0,
          Bmax = 1.38629436111989,
          yi.name = "yi",
          vi.name = "vi",
          dat = d,
          R = 500)
## [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
## None of the pointwise confidence intervals was estimable via bias-corrected and accelerated bootstrapping, so the confidence band on the plot is omitted. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.

### R output:
# [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
# None of the pointwise confidence intervals were not estimable via bias-corrected and accelerated bootstrapping, so the confidence band on the plot is omitted. You can try increasing R.

### Website output:
# pass input on log scale
knitr::include_graphics("mytest-expected/005.png")

test5 flegal_prepped.csv file - correct

d = read.csv("Datasets for website test/flegal_prepped.csv")
# note: log(0.5) = -0.6931472
# log(1.5) = 0.4054651
confounded_meta(method="calibrated",
                q = log(.5),
                r = 0.1,
                muB = log(1.5),
                tail = "above",
                yi.name = "yi",
                vi.name = "vi",
                dat = d,
                R = 500)
## [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
## The confidence interval and/or standard error for the proportion were not estimable via bias-corrected and accelerated bootstrapping. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.
##   Value      Est         SE    CI.lo    CI.hi
## 1  Prop 1.000000 0.00000000       NA       NA
## 2  Tmin 2.177725 0.08806265 2.058396 2.350757
## 3  Gmin 3.779212 0.18013471 3.534404 4.132695
### R output:
# [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
# The confidence interval and/or standard error for the proportion were not estimable via bias-corrected and accelerated bootstrapping. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.
#   Value      Est         SE    CI.lo    CI.hi
# 1  Prop 1.000000 0.00000000       NA       NA
# 2  Tmin 2.177725 0.08883057 2.061940 2.353965
# 3  Gmin 3.779212 0.18173756 3.541689 4.139234

### Website output: 
# pass on log scale
knitr::include_graphics("mytest-expected/006.png")

# pass on RR scale
knitr::include_graphics("mytest-expected/007.png")

test6 flegal_prepped.csv file - correct

d = read.csv("Datasets for website test/flegal_prepped.csv")

confounded_meta(method="calibrated",
                q = log(1.2),
                r = 1.0,
                muB = log(1),
                tail = "above",
                yi.name = "yi",
                vi.name = "vi",
                dat = d,
                R = 500)
## [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
## Prop is already less than or equal to r even with no confounding, so Tmin and Gmin are simply equal to 1. No confounding at all is required to make the specified shift.
##   Value        Est         SE       CI.lo      CI.hi
## 1  Prop 0.03571429 0.01643293 0.007142857 0.07142857
## 2  Tmin 1.00000000 0.00000000 1.000000000         NA
## 3  Gmin 1.00000000 0.00000000 1.000000000         NA
### R output:
# [1] "All values of t are equal to  1 \n Cannot calculate confidence intervals"
# Prop is already less than or equal to r even with no confounding, so Tmin and Gmin are simply equal to 1. No confounding at all is required to make the specified shift.
#   Value        Est         SE       CI.lo      CI.hi
# 1  Prop 0.03571429 0.01584981 0.007142857 0.06428571
# 2  Tmin 1.00000000 0.00000000 1.000000000         NA
# 3  Gmin 1.00000000 0.00000000 1.000000000         NA

### Website output: 
knitr::include_graphics("mytest-expected/008.png")

test7 data_calib_test_1-1.csv file - correct

d = read.csv("Datasets for website test/data_calib_test_1-1.csv")

## an example with crazy input
confounded_meta(method="calibrated",
                q = log(0),
                r = 0,
                muB = log(1),
                tail = "above",
                yi.name = "est",
                vi.name = "var",
                dat = d,
                R = 0)
## [1] "All values of t are equal to  NaN \n Cannot calculate confidence intervals"
## The confidence interval and/or standard error for the proportion were not estimable via bias-corrected and accelerated bootstrapping. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.
## [1] "All values of t are equal to  NaN \n Cannot calculate confidence intervals"
## The confidence interval and/or standard error for Tmin and Gmin were not estimable via bias-corrected and accelerated bootstrapping. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.
##   Value Est SE CI.lo CI.hi
## 1  Prop   1 NA    NA    NA
## 2  Tmin Inf NA    NA    NA
## 3  Gmin NaN NA    NA    NA
### R output:
# [1] "All values of t are equal to  NaN \n Cannot calculate confidence intervals"
# The confidence interval and/or standard error for the proportion were not estimable via bias-corrected and accelerated bootstrapping. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.
# [1] "All values of t are equal to  NaN \n Cannot calculate confidence intervals"
# The confidence interval and/or standard error for Tmin and Gmin were not estimable via bias-corrected and accelerated bootstrapping. You can try increasing the number of bootstrap iterates or choosing a less extreme threshold.
#   Value Est SE CI.lo CI.hi
# 1  Prop   1 NA    NA    NA
# 2  Tmin Inf NA    NA    NA
# 3  Gmin NaN NA    NA    NA

sens_plot(method="calibrated",
          type = "line",
          q = log(0),
          tail = "above",
          Bmin = log(0),
          Bmax = log(0),
          yi.name = "yi",
          vi.name = "vi",
          dat = d,
          R = 0)
## Error in seq.default(Bmin, Bmax, 0.01): 'from' must be a finite number
### R output:
# Error in seq.default(Bmin, Bmax, 0.01) : 'from' must be a finite number 

### Website output: 
knitr::include_graphics("mytest-expected/009.png")

test8 - correct

## parametric method test
confounded_meta(method="parametric",
                q=log(1.1),
                r=0.2,
                tail="above",
                muB=log(1.2),
                sigB=sqrt(0.35*0.1),
                yr=log(1.2),
                vyr=0.01,
                t2=0.1,
                vt2=0.01)
##   Value       Est        SE    CI.lo     CI.hi
## 1  Prop 0.3542627 0.1809323 0.000000 0.7088834
## 2  Tmin 1.4235523 0.2369612 1.000000 1.8879878
## 3  Gmin 2.2000501 0.5187985 1.183224 3.2168766
### R output:
#   Value       Est        SE    CI.lo     CI.hi
# 1  Prop 0.3542627 0.1809323 0.000000 0.7088834
# 2  Tmin 1.4235523 0.2369612 1.000000 1.8879878
# 3  Gmin 2.2000501 0.5187985 1.183224 3.2168766

sens_plot(method = "parametric",
          type="line",
          q=log(1.1),
          yr=log(1.2),
          vyr=0.01,
          t2=0.1,
          vt2=0.01,
          Bmin=log(1),
          Bmax=log(4),
          sigB=sqrt(0.35*0.1),
          tail="above" )
## Warning in sens_plot(method = "parametric", type = "line", q = log(1.1), :
## Calculating parametric confidence intervals in the plot. For values of the
## proportion that are less than 0.15 or greater than 0.85, these confidence
## intervals may not perform well.

### R output:
# Warning message:
# In sens_plot(method = "parametric", type = "line", q = log(1.1),  :
#   Calculating parametric confidence intervals in the plot. For values of Phat that are less than 0.15 or greater than 0.85, these confidence intervals may not perform well.

### Website output:
# passing input on RR scale
knitr::include_graphics("mytest-expected/010.png")

# passing input on log-RR scale

knitr::include_graphics("mytest-expected/011.png")

test9 - correct

## parametric method test
## see what errors if all 0
confounded_meta(method="parametric",
                q=log(0),
                r=0,
                tail="above",
                muB=log(0),
                sigB=0,
                yr=log(0),
                vyr=0,
                t2=0,
                vt2=0)
## Error in confounded_meta(method = "parametric", q = log(0), r = 0, tail = "above", : Must have muB > 0. Use the muB.toward.null argument instead if you want to consider bias away from the null. See Details.
### R output:
# Error in confounded_meta(method = "parametric", q = log(0), r = 0, tail = "above",  : 
#   Must have muB > 0. Use the muB.toward.null argument instead if you want to consider bias away from the null. See Details.

### Website output: 
knitr::include_graphics("mytest-expected/012.png")

test10 - correct

# note:
# log(0.5) = -0.6931472
# log(1.5) = 0.4054651
confounded_meta(method="parametric",
                q=log(.5),
                r=0.75,
                tail="below",
                muB=log(1.5),
                sigB=sqrt(0.5*0.25),
                yr=log(1.5),
                vyr=0.5,
                t2=0.25,
                vt2=0.5)
## Warning in confounded_meta(method = "parametric", q = log(0.5), r = 0.75, : Prop
## is close to 0 or 1. We recommend choosing method = "calibrated" or alternatively
## using bias-corrected and accelerated bootstrapping to estimate all inference in
## this case.
## Prop is already less than or equal to r even with no confounding, so Tmin and Gmin are simply equal to 1. No confounding at all is required to make the specified shift.
##   Value        Est         SE      CI.lo      CI.hi
## 1  Prop 0.02496774 0.00057182 0.02384699 0.02608848
## 2  Tmin 1.00000000         NA         NA         NA
## 3  Gmin 1.00000000         NA         NA         NA
### R output:
# Prop is already less than or equal to r even with no confounding, so Tmin and Gmin are simply equal to 1. No confounding at all is required to make the specified shift.
#   Value        Est         SE      CI.lo      CI.hi
# 1  Prop 0.02496774 0.00057182 0.02384699 0.02608848
# 2  Tmin 1.00000000         NA         NA         NA
# 3  Gmin 1.00000000         NA         NA         NA
# Warning message:
# In confounded_meta(method = "parametric", q = log(0.5), r = 0.75,  :
#   Prop is close to 0 or 1. We recommend choosing method = "calibrated" or alternatively using bias-corrected and accelerated bootstrapping to estimate all inference in this case.

sens_plot(method = "parametric",
          type="line",
          q=log(.5),
          yr=log(1.5),
          vyr=0.5,
          t2=0.25,
          vt2=sqrt(0.5*(0.25)),
          Bmin=log(1),
          Bmax=log(4),
          sigB=sqrt(0.5*0.25),
          tail="below" )
## Warning in sens_plot(method = "parametric", type = "line", q = log(0.5), :
## Calculating parametric confidence intervals in the plot. For values of the
## proportion that are less than 0.15 or greater than 0.85, these confidence
## intervals may not perform well.

### R output:
# Warning message:
# In sens_plot(method = "parametric", type = "line", q = log(0.5),  :
#   Calculating parametric confidence intervals in the plot. For values of Phat that are less than 0.15 or greater than 0.85, these confidence intervals may not perform well.

### Website output: 
knitr::include_graphics("mytest-expected/013.png")

test11 - correct

## on log-RR scale
# log(1.2)
## parametric method test
confounded_meta(method="parametric",
                q=0.1823216,
                r=0.2,
                tail="below",
                muB=.2,
                sigB=sqrt(0.15*0.25),
                yr=.4,
                vyr=0.05,
                t2=0.25,
                vt2=0.05)
##   Value       Est        SE    CI.lo     CI.hi
## 1  Prop 0.4847044 0.1799780 0.131954 0.8374548
## 2  Tmin 1.2252345 0.3580879 1.000000 1.9270738
## 3  Gmin 1.7505582 0.8524453 1.000000 3.4213202
### R output:
#   Value       Est        SE     CI.lo     CI.hi
# 1  Prop 0.4847044 0.1935404 0.1053722 0.8640366
# 2  Tmin 1.2252345 0.5534300 1.0000000 2.3099373
# 3  Gmin 1.7505582 1.3174664 1.0000000 4.3327449

sens_plot(method = "parametric",
          type="line",
          q=0.1823216,
          yr=.4,
          vyr=0.05,
          t2=0.25,
          vt2=0.05,
          Bmin=0,
          Bmax=1.791759,
          sigB=sqrt(0.15*0.25),
          tail="below" )
## Warning in sens_plot(method = "parametric", type = "line", q = 0.1823216, :
## Calculating parametric confidence intervals in the plot. For values of the
## proportion that are less than 0.15 or greater than 0.85, these confidence
## intervals may not perform well.

### R output:
# Warning message:
# In sens_plot(method = "parametric", type = "line", q = 0.1823216,  :
#   Calculating parametric confidence intervals in the plot. For values of Phat that are less than 0.15 or greater than 0.85, these confidence intervals may not perform well.

### Website output: 
knitr::include_graphics("mytest-expected/014.png")

test12 kodama_prepped.csv - correct

# MM did this one
d = read.csv("Datasets for website test/kodama_prepped.csv")

confounded_meta(method="calibrated",
                q=log(1.5),
                r=0.3,
                tail="below",
                muB=log(1.5),
                
                dat = d, 
                yi.name = "yi",
                vi.name = "vi")
## Warning in norm.inter(t, adj.alpha): extreme order statistics used as endpoints
##   Value      Est         SE CI.lo    CI.hi
## 1  Prop 0.937500 0.10273108     0 1.000000
## 2  Tmin 1.003351 0.02520898     1 1.130696
## 3  Gmin 1.061336 0.12008544     1 1.515113
### R output:
#   Value      Est         SE  CI.lo    CI.hi
# 1  Prop 0.937500 0.09653012 0.4375 1.000000
# 2  Tmin 1.003351 0.02353298 1.0000 1.119580
# 3  Gmin 1.061336 0.11581616 1.0000 1.485475
# Warning message:
# In norm.inter(t, adj.alpha) : extreme order statistics used as endpoints

### Website output: 
knitr::include_graphics("mytest-expected/015.png")